/*
 * Copyright (c) 2012 swing-on-fire Team
 * 
 * This file is part of Swing-On-Fire (http://code.google.com/p/swing-on-fire), licensed under the terms of the MIT
 * License (MIT).
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.google.code.swingonfire.showcase;

import static com.google.code.swingonfire.builder.JComponentBuilders.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.event.WindowEvent;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;

import com.google.code.eventsonfire.Events;
import com.google.code.eventsonfire.awt.AWTEvents;
import com.google.code.eventsonfire.swing.SwingEventHandler;
import com.google.code.eventsonfire.swing.SwingEvents;
import com.google.code.swingonfire.border.ShadowBorder;
import com.google.code.swingonfire.component.page.JPagePane;
import com.google.code.swingonfire.component.page.JPageComponentAnimations;

public class ShowcaseFrame extends JFrame
{

    private static final long serialVersionUID = 2824710189905258808L;

    private final JList showcaseList;
    private final JPagePane showcasePane;

    public ShowcaseFrame(GraphicsConfiguration graphicsConfiguration) throws HeadlessException
    {
        super("Swing on Fire - Showcase", graphicsConfiguration);

        setLayout(new BorderLayout());
        addWindowListener(AWTEvents.fireOnWindowClosing());

        showcaseList =
            Events.bind(
                listBuilder().items(ShowcaseSamples.values()).listener(SwingEvents.fireOnListSelection())
                    .preferredSize(192, 256).selected(ShowcaseSamples.JCALENDAR)
                    .cellRenderer(new ShowcaseListCellRenderer()).background(Color.WHITE).build(), this);

        //        showcasePane =
        //        pagePaneBuilder().animationFactoryForAnyNext(JPageTransitionAnimationFactory.fadeInFromTopAndShrink(0.5))
        //        .animationFactoryForAnyPrevious(JPageTransitionAnimationFactory.fadeInFromBottomAndShrink(0.5)).build();
        //        showcasePane =
        //            new JPagePaneBuilder().animationFactory(JPageTransitionAnimationFactory.fadeInAndShrink(0.5))
        //                .animationFactoryForNext(JPageTransitionAnimationFactory.moveInFromTop(0.5))
        //                .animationFactoryForPrevious(JPageTransitionAnimationFactory.moveInFromBottom(0.5)).build();

        showcasePane = new JPagePane();
        showcasePane.setAnimation(JPageComponentAnimations.vertical());
        showcasePane.setAnimationDuration(1);

        for (ShowcaseSamples sample : ShowcaseSamples.values())
        {
            JComponent instance = sample.getInstance();

            if (instance != null)
            {
                showcasePane.add(instance);
            }
        }

        add(scrollPaneBuilder().view(showcaseList).border(ShadowBorder.create()).build(), BorderLayout.WEST);
        add(showcasePane);

        Events.bind(this, this);

    }

    @SwingEventHandler(event = ListSelectionEvent.class)
    public void onSelection()
    {
        ShowcaseSamples showcase = (ShowcaseSamples) showcaseList.getSelectedValue();

        if (showcase != null)
        {
            int index = showcasePane.indexOf(showcase.getName());

            if (index >= 0)
            {
                showcasePane.setSelectedComponentIndex(index);
            }
        }
    }

    @SwingEventHandler(event = WindowEvent.class)
    public void onClosing()
    {
        System.exit(0);
    }

}
